package customize

import (
	"gitcode.com/G-YT/baoyou/server/global"
	"gitcode.com/G-YT/baoyou/server/model/common/request"
	"gitcode.com/G-YT/baoyou/server/model/customize"
	customizeReq "gitcode.com/G-YT/baoyou/server/model/customize/request"
	"gorm.io/gorm"
)

type SolarTermsService struct {
}

// CreateSolarTerms 创建节气记录
// Author [piexlmax](https://github.com/piexlmax)
func (solarTermsService *SolarTermsService) CreateSolarTerms(solarTerms *customize.SolarTerms) (err error) {
	err = global.GVA_DB.Create(solarTerms).Error
	return err
}

// DeleteSolarTerms 删除节气记录
// Author [piexlmax](https://github.com/piexlmax)
func (solarTermsService *SolarTermsService) DeleteSolarTerms(solarTerms customize.SolarTerms) (err error) {
	err = global.GVA_DB.Delete(&solarTerms).Error
	return err
}

// DeleteSolarTermsByIds 批量删除节气记录
// Author [piexlmax](https://github.com/piexlmax)
func (solarTermsService *SolarTermsService) DeleteSolarTermsByIds(ids request.IdsReq) (err error) {
	err = global.GVA_DB.Delete(&[]customize.SolarTerms{}, "id in ?", ids.Ids).Error
	return err
}

// UpdateSolarTerms 更新节气记录
// Author [piexlmax](https://github.com/piexlmax)
func (solarTermsService *SolarTermsService) UpdateSolarTerms(solarTerms customize.SolarTerms) (err error) {
	err = global.GVA_DB.Save(&solarTerms).Error
	return err
}

// GetSolarTerms 根据id获取节气记录
// Author [piexlmax](https://github.com/piexlmax)
func (solarTermsService *SolarTermsService) GetSolarTerms(id uint) (solarTerms customize.SolarTerms, err error) {
	err = global.GVA_DB.Where("id = ?", id).First(&solarTerms).Error
	return
}

// GetSolarTermsInfoList 分页获取节气记录
// Author [piexlmax](https://github.com/piexlmax)
func (solarTermsService *SolarTermsService) GetSolarTermsInfoList(info customizeReq.SolarTermsSearch) (list []customize.SolarTerms, total int64, err error) {
	limit := info.PageSize
	offset := info.PageSize * (info.Page - 1)
	// 创建db
	db := global.GVA_DB.Model(&customize.SolarTerms{})

	// 如果有条件搜索 下方会自动创建搜索语句
	err = db.Count(&total).Error
	if err != nil {
		return
	}

	if limit != 0 {
		db = db.Limit(limit).Offset(offset)
	}

	err = db.Find(&list).Error
	return list, total, err
}

func (solarTermsService *SolarTermsService) SetSolarTermsArticle(req customizeReq.SolarTermsArticleReq) (err error) {
	return global.GVA_DB.Transaction(func(tx *gorm.DB) error {
		if err := tx.Where("solar_terms_id = ?", req.SolarTermsID).Delete(&customize.SolarTermsArticle{}).Error; err != nil {
			return err
		}
		var solarTermsArticles []customize.SolarTermsArticle
		for _, richText := range req.RichTexts {
			solarTermsArticles = append(solarTermsArticles, customize.SolarTermsArticle{
				SolarTermsID: req.SolarTermsID,
				ArticleID:    richText,
				Type:         "richText",
			})
		}
		for _, audio := range req.Audios {
			solarTermsArticles = append(solarTermsArticles, customize.SolarTermsArticle{
				SolarTermsID: req.SolarTermsID,
				ArticleID:    audio,
				Type:         "audio",
			})
		}
		for _, video := range req.Videos {
			solarTermsArticles = append(solarTermsArticles, customize.SolarTermsArticle{
				SolarTermsID: req.SolarTermsID,
				ArticleID:    video,
				Type:         "video",
			})
		}
		if len(solarTermsArticles) == 0 {
			return nil
		}
		return tx.Create(&solarTermsArticles).Error
	})
}

func (solarTermsService *SolarTermsService) GetSolarTermsArticle(SolarTermsID string) (SolarTermsArticle []customize.SolarTermsArticle, err error) {
	err = global.GVA_DB.Where("solar_terms_id = ?", SolarTermsID).Find(&SolarTermsArticle).Error
	return
}
